package cn.ibizlab.odoo.core.odoo_project.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Iproject_task_type;
import cn.ibizlab.odoo.core.odoo_project.domain.Project_task_type;
import cn.ibizlab.odoo.core.odoo_project.filter.Project_task_typeSearchContext;
import cn.ibizlab.odoo.core.odoo_project.service.IProject_task_typeService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[任务阶段] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Project_task_typeServiceImpl implements IProject_task_typeService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Iproject_task_typeClientService project_task_typeClientService;

    @Override
    public Project_task_type get(Integer id) {
        Iproject_task_type clientModel = project_task_typeClientService.createModel();
        clientModel.setId(id);
        project_task_typeClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Project_task_type get(Project_task_type project_task_type) {
        Project_task_type retproject_task_type = this.get(project_task_type.getId()) ;
    	BeanCopier copier=BeanCopier.create(retproject_task_type.getClass(), project_task_type.getClass(), false);
        copier.copy(retproject_task_type, project_task_type, null);
        return project_task_type ;
    }

    @Override
    public boolean create(Project_task_type project_task_type) {
        boolean bOk = false ;
        Iproject_task_type clientModel = this.convert2Model(project_task_type,null);
        project_task_typeClientService.create(clientModel) ;
        this.convert2Domain(clientModel,project_task_type);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Iproject_task_type clientModel = project_task_typeClientService.createModel();
        clientModel.setId(id);
        project_task_typeClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean update(Project_task_type project_task_type) {
        boolean bOk = false ;
        Iproject_task_type clientModel = this.convert2Model(project_task_type,null);
        project_task_typeClientService.update(clientModel) ;
        this.convert2Domain(clientModel,project_task_type) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Project_task_type> project_task_types, int batchSize) {

                        //project_task_typeClientService.createBatch(project_task_types,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //project_task_typeClientService.removeBatch(project_task_types) ;

    }

    @Override
    public void updateBatch(List<Project_task_type> project_task_types, int batchSize) {

        //project_task_typeClientService.updateBatch(project_task_types,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Project_task_type> searchDefault(Project_task_typeSearchContext context) {
        List<Project_task_type> list = new ArrayList<Project_task_type>();
        Page<cn.ibizlab.odoo.core.client.model.Iproject_task_type> clientModelList = project_task_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproject_task_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Project_task_type>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据EMail模板查询
     */
    @Override
    public Page<Project_task_type> selectByOdoo_mail_template(Integer id){
        Project_task_typeSearchContext context = new Project_task_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Project_task_type> list = new ArrayList<Project_task_type>();
        Page<cn.ibizlab.odoo.core.client.model.Iproject_task_type> clientModelList = project_task_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproject_task_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Project_task_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据EMail模板查询
     */
    @Override
    public Page<Project_task_type> selectByOdoo_rating_template(Integer id){
        Project_task_typeSearchContext context = new Project_task_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Project_task_type> list = new ArrayList<Project_task_type>();
        Page<cn.ibizlab.odoo.core.client.model.Iproject_task_type> clientModelList = project_task_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproject_task_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Project_task_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Project_task_type> selectByOdoo_create(Integer id){
        Project_task_typeSearchContext context = new Project_task_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Project_task_type> list = new ArrayList<Project_task_type>();
        Page<cn.ibizlab.odoo.core.client.model.Iproject_task_type> clientModelList = project_task_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproject_task_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Project_task_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Project_task_type> selectByOdoo_write(Integer id){
        Project_task_typeSearchContext context = new Project_task_typeSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Project_task_type> list = new ArrayList<Project_task_type>();
        Page<cn.ibizlab.odoo.core.client.model.Iproject_task_type> clientModelList = project_task_typeClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Iproject_task_type clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Project_task_type>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Iproject_task_type convert2Model(Project_task_type domain , Iproject_task_type model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = project_task_typeClientService.createModel() ;
        }

         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getAuto_validation_kanban_stateDirtyFlag())
            model.setAuto_validation_kanban_state(domain.getAuto_validation_kanban_state());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getProject_idsDirtyFlag())
            model.setProject_ids(domain.getProject_ids());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getDescriptionDirtyFlag())
            model.setDescription(domain.getDescription());
         if(domain.getLegend_normalDirtyFlag())
            model.setLegend_normal(domain.getLegend_normal());
         if(domain.getFoldDirtyFlag())
            model.setFold(domain.getFold());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getLegend_blockedDirtyFlag())
            model.setLegend_blocked(domain.getLegend_blocked());
         if(domain.getLegend_priorityDirtyFlag())
            model.setLegend_priority(domain.getLegend_priority());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getLegend_doneDirtyFlag())
            model.setLegend_done(domain.getLegend_done());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getRating_template_id_textDirtyFlag())
            model.setRating_template_id_text(domain.getRating_template_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getMail_template_id_textDirtyFlag())
            model.setMail_template_id_text(domain.getMail_template_id_text());
         if(domain.getMail_template_idDirtyFlag())
            model.setMail_template_id(domain.getMail_template_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getRating_template_idDirtyFlag())
            model.setRating_template_id(domain.getRating_template_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Project_task_type convert2Domain( Iproject_task_type model ,Project_task_type domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Project_task_type();
        }

        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getAuto_validation_kanban_stateDirtyFlag())
            domain.setAuto_validation_kanban_state(model.getAuto_validation_kanban_state());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getProject_idsDirtyFlag())
            domain.setProject_ids(model.getProject_ids());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getDescriptionDirtyFlag())
            domain.setDescription(model.getDescription());
        if(model.getLegend_normalDirtyFlag())
            domain.setLegend_normal(model.getLegend_normal());
        if(model.getFoldDirtyFlag())
            domain.setFold(model.getFold());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getLegend_blockedDirtyFlag())
            domain.setLegend_blocked(model.getLegend_blocked());
        if(model.getLegend_priorityDirtyFlag())
            domain.setLegend_priority(model.getLegend_priority());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getLegend_doneDirtyFlag())
            domain.setLegend_done(model.getLegend_done());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getRating_template_id_textDirtyFlag())
            domain.setRating_template_id_text(model.getRating_template_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getMail_template_id_textDirtyFlag())
            domain.setMail_template_id_text(model.getMail_template_id_text());
        if(model.getMail_template_idDirtyFlag())
            domain.setMail_template_id(model.getMail_template_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getRating_template_idDirtyFlag())
            domain.setRating_template_id(model.getRating_template_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
